define(
	[
		'require'
	],
	function(require){
		 function actionFormatter(value, row, index) {

	        return '<div class="" style="text-align:left">'+APM.util.escape(value)+'</div>';
	    }
		var Controller = Ember.Controller.extend({
			init:function(){
				this.reportformData();
			},
			startDate:Ember.computed(function(){
				var date = new Date(new Date() - timeD_value - 3600000*24-60000);
				date.setHours(0);
				date.setMinutes(0);
				date.setSeconds(0);
				date.setMilliseconds(0);
				date.setSeconds(0);
				date.setMilliseconds(0);
				date.setDate(date.getDate()+1);
				return new Date(+date);

			}),
			endDate:Ember.computed(function(){
				var date = new Date(new Date() - timeD_value-60000);
				date.setHours(0);
				date.setMinutes(0);
				date.setSeconds(0);
				date.setMilliseconds(0);
				date.setSeconds(0);
				date.setMilliseconds(0);
				date.setDate(date.getDate()+1);
				return new Date(+date);
			}),
			formTitle: "系统性能报表",

			throughputData: ["system/throughput/total_minute"],
			businessData: ["business/throughput/total_minute"],
			systemResponseTimeData: ["system/response_time/avg"],
			responseTimeData: ["business/response_time/avg"],
			systemSuccessRateData: ["system/success/rate"],
			successRateData: ["business/success/rate"],
			
			columns: [
			  [  {
			    "field": "applicationName",
			    "title": "系统名称",
			    formatter:actionFormatter,
			    "colspan": 1,
			    "rowspan": 2,
			    // "sortable": true

			  }
			  , {
			    "title": "系统级",
			    "colspan": 6,
			    "rowspan": 1
			  },
			  {
			    "title": "交易级",
			    "colspan": 6,
			    "rowspan": 1
			  }], 
			  [{
			    "field": "system/throughput/total",
			    "title": "总吞吐量",
			    "colspan": 1,
			    "rowspan": 1,
			    // "sortable": true
			  }, {
			    "field": "system/throughput/total_minute",
			    "title": "吞吐量/分钟",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "system/response_time/avg",
			    "title": "平均响应时间(ms)",
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "system/success/rate",
			    "title": "成功率%",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "system/error/count",
			    "title": "错误数",
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "system/error/count_minute",
			    "title": "错误数/分钟",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "business/throughput/total",
			    "title": "总交易量",
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "business/throughput/total_minute",
			    "title": "交易量/分钟",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "business/response_time/avg",
			    "title": "平均响应时间(ms)",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "business/success/rate",
			    "title": "成功率%",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "business/error/count",
			    "title": "错误数",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "business/error/count_minute",
			    "title": "错误数/分钟",
			    "colspan": 1,
			    "rowspan": 1,
			  }]
			],
			//根据数据来填充表格里的内容
			fillTableContent: function(node,index){
				$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
				var tableTr = $(".table-left-container .tbody .table-left-tr").eq(index);
				$(".table-left-container .tbody .table-left-tr").eq(index).css({"height":(node.channel.length+1)*50+"px","line-height":(node.channel.length+1)*50+"px"});
				if(node.typeName){
					$("<div class='table-left-td'>"+node.typeName+"</div>").appendTo(tableTr);
				}

				if(node.serviceName){
					$("<div class='table-left-td'>"+node.serviceName+"</div>").appendTo(tableTr);
				}
			},
			//根据数据插入表格头部
			fillTableHeader: function(){
				$(".table-left-container .thead").find("div").remove();
				$(".table-left-container .thead").append("<div class='clearfix table-left-tr'></div>");
				var tableTr = $(".table-left-container .thead .table-left-tr");
				$("<div class='table-left-td pull-left'>交易类型</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>服务节点</div>").appendTo(tableTr);
			},
			reportformData: function(){
		
				var ctrl = this;
				var params = {
					"startDate": +this.get("startDate"),
					"endDate": +this.get("endDate")
				}
				var newArray = [],
					newObject;
				ctrl.set("tipTitle","");

				return this.store.ajax("GET", "report/system/performance",params)
				.then(function(response){
					var performances = response.performances;
					if(performances[0]){
		
					}else{
						ctrl.set("tipTitle","这段时间内暂无数据");
					}
					ctrl.set("groupData",performances);
				});
			},
			url:"report/system/performance/download",
			actions: {
				timeRangesChange: function(timeRange){
					this.set("startDate",timeRange.startDate);
					this.set("endDate",timeRange.endDate);

					this.reportformData();
				},
				export: function(){
					var ctrl = this,
						appId = ctrl.get("applicationValue"),
						startDate = +ctrl.get("startDate"),
						endDate = +ctrl.get("endDate");
						
					confirmDialog("确定要导出报表吗？")
					.then(function(){
						var url="/front/rest/apm/"+ctrl.get("url")+"?appId="+appId+"&startDate="+startDate+"&endDate="+endDate+"&token="+encodeURIComponent($.cookie("token"));
						window.location.href =url;
					});
				}
			}
		});

		APM.FormSystemperformanceController = Controller;
		return Controller;
	}
);